﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BacteriaSimulation.Simulation
{
    public class UpdateBacteriumPositionTask : ITask
    {
        void ITask.executeTask(Simulation simulation, Bacterium bacterium, SimulationEnvironment env)
        {

            double period = simulation.SimulationPeriodSec;

            //computes the shift given by the currente speed vector
            double shiftX = bacterium.Speed.XComponent * period;
            double shiftY = bacterium.Speed.YComponent * period;
            Vector2D shift1 = new Vector2D(shiftX, shiftY);

            // Computes the shift due to the current acceleration
            UnitVector2D directionVect = bacterium.AccelerationDirection;
            Double speedIncrementMagnitude = bacterium.AccelerationMagnitude * period;
            // the speed components along the direction of the acceleration
            double speedIncrementXComp = speedIncrementMagnitude * directionVect.XComponent;
            double speedIncrementYComp = speedIncrementMagnitude * directionVect.YComponent;

            double shiftIncrementXComp = .5 * speedIncrementXComp * period;
            double shiftIncrementYComp = .5 * speedIncrementYComp * period;

            //Vector2D speedIncrementVector = new Vector2D(speedIncrementXComp, speedIncrementYComp);
            Vector2D resultingSpeedVector = new Vector2D(speedIncrementXComp + bacterium.Speed.XComponent,
                speedIncrementYComp + bacterium.Speed.YComponent);

            resultingSpeedVector = Vector2D.normalizeToMagnitude(resultingSpeedVector, bacterium.SpeedMagnitude);

            Vector2D resultingShiftVector = new Vector2D(shiftX + shiftIncrementXComp,
                shiftY + shiftIncrementYComp);

            bacterium.Speed = resultingSpeedVector;
            bacterium.X += resultingShiftVector.XComponent;
            bacterium.Y += resultingShiftVector.YComponent;
        }
    }
}
